﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Delay;
using System.ComponentModel;
using System.IO.IsolatedStorage;
using System.Threading;
using System.Windows.Navigation;

namespace PhoneInfo.Page
{
    public partial class SeparatorCenter : PhoneApplicationPage
    {
        static readonly string[] makes = { "不限", "诺基亚","HTC", "摩托罗拉", "索尼爱立信", "Apple", "小米", 
                                             "魅族", "联想", "LG", "华为", "中兴", "OPPO"};
        static readonly string[] appearance = { "不限", "直板", "翻盖", "滑盖", "侧滑盖" };
        static readonly string[] system = { "不限", "塞班(Symbian)", "安卓(Android)", "微软(windows)", "Linux", "MTK", "其他", "苹果(Apple)" };
        static readonly string[] price = { "不限", "1000元以下", "1000元~2000元", "2000元~3000元", "3000元以上" };
        static readonly string[] operators = { "不限", "移动G3(TD-SCDMA)", "联通沃(WCDMA)", "电信3G(CDMA2000)", "GSM(2G)", "GSM + CDMA(双网)", "GSM + CDMA2000(双网)", "双卡双待" };
        static readonly string[] ram = { "不限", "2GB", "1GB", "512MB", "256MB", "128MB" };
        static readonly string[] cpu = { "不限", "更高", "1.4GHZ", "1.2GHZ", "1GHZ", "800MHZ", "600MHZ", "双核", "单核" };
        static readonly string[] dpi = { "不限", "240*320", "320*480", "360*640", "480*800", "480*854", "540*960", "640*960", "800*1280" };
        static readonly string[] screen = { "不限", "9寸以上", "7寸", "5寸", "4寸", "3寸", "2寸以下" };
        static readonly string[] color = { "不限", "1600万色", "26万色", "65536色", "4096色", "单色" };
        //这里还应该有一个品牌的数组

        private BackgroundWorker bkgWork0 = null;
        private ProgressIndicator _progress = new ProgressIndicator() { IsVisible = false, IsIndeterminate = true };
        public SeparatorCenter()
        {
            InitializeComponent();
            this.SetValue(SystemTray.OpacityProperty, 0.00001);
            //异步取出结果；
            bkgWork0 = new BackgroundWorker();
            RunBkgWorker(bkgWork0);
        }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            SystemTray.ProgressIndicator = this._progress;
            this._progress.IsVisible = false;
        }
        private void RunBkgWorker(BackgroundWorker bkgWork)
        {
            bkgWork.DoWork += ((s, args) => { Thread.Sleep(5); });
            bkgWork.RunWorkerCompleted += ((s, args) =>
            {
                for (int i = 0; i < 14; i++)
                {
                    switch (i)
                    {
                        case 0:
                            Make.Visibility = Visibility.Visible;
                            break;
                        case 1:
                            Price.Visibility = Visibility.Visible;
                            break;
                        case 2:
                            OpraterSystem.Visibility = Visibility.Visible;
                            break;
                        case 3:
                            Appearance.Visibility = Visibility.Visible;
                            break;
                        case 4:
                            GPS.Visibility = Visibility.Visible;
                            break;
                        case 5:
                            WIFI.Visibility = Visibility.Visible;
                            break;
                        case 6:
                            Opraters.Visibility = Visibility.Visible;
                            break;
                        case 7:
                            MarketTime.Visibility = Visibility.Visible;
                            break;
                        case 8:
                            Ram.Visibility = Visibility.Visible;
                            break;
                        case 9:
                            Area.Visibility = Visibility.Visible;
                            break;
                        case 10:
                            CPU.Visibility = Visibility.Visible;
                            break;
                        case 11:
                            DPI.Visibility = Visibility.Visible;
                            break;
                        case 12:
                            Screen.Visibility = Visibility.Visible;
                            break;
                        case 13:
                            Color.Visibility = Visibility.Visible;
                            break;
                    }
                }
            });
            bkgWork.RunWorkerAsync();
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            Make.DataContext = makes;
            OpraterSystem.DataContext = system;
            Price.DataContext = price;
            Appearance.DataContext = appearance;
            Color.DataContext = color;
            Screen.DataContext = screen;
            DPI.DataContext = dpi;
            CPU.DataContext = cpu;
            Ram.DataContext = ram;
            _progress.IsVisible = false;
            Opraters.DataContext = operators;
            if (App.appSitting.Theme == 1)
                ApplicationBar = (ApplicationBar)this.Resources["AppBar"];
            else
                ApplicationBar = (ApplicationBar)this.Resources["AppBar1"];
        }
        void shrewd_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (sender == null) return;
            SearchResultView se = sender as SearchResultView;
            if (se != null)
            {
                if (e.PropertyName == "Result") _progress.IsVisible = false;
                return;
            }
            phonePkView pk = sender as phonePkView;
            if (pk != null)
            {
                if (e.PropertyName == "ImageVis")
                {
                    if (pk.ImageVis == true) _progress.IsVisible = false;
                    if (pk.ImageVis == false) _progress.IsVisible = true;
                }
                return;
            }
        }
        private void Reply(object sender, EventArgs e)
        {
            //搜索
            string _default = "0";
            if (sender == null) return;
            string make = (string)Make.SelectedItem;
            string price = Price.SelectedIndex.ToString();
            string system = OpraterSystem.SelectedIndex.ToString();
            string stype = Appearance.SelectedIndex.ToString();
            string camera = DPI.SelectedIndex.ToString();
            string gps = "";
            if (GPS.SelectedIndex == 1) gps = GPS.SelectedIndex.ToString();
            string wifi = "";
            if (GPS.SelectedIndex == 1) wifi = (string)GPS.SelectedIndex.ToString();
            string carrier = Opraters.SelectedIndex.ToString();
            string markettime = MarketTime.SelectedIndex.ToString();
            string ram = Ram.SelectedIndex.ToString();
            string area = Area.SelectedIndex.ToString();
            string cpu = CPU.SelectedIndex.ToString();
            string distingwish = DPI.SelectedIndex.ToString();
            string screensize = Screen.SelectedIndex.ToString();
            string color = Color.SelectedIndex.ToString();

            //跳转到最新和最热的评论结果
            string page = "/Page/SearchResult.xaml";
            page += "?evry=" + "evry";
            if (make != "不限")
                page += "&ppname=" + make;
            if (price != _default)
                page += "&price=" + price;
            if (system != _default)
                page += "&system=" + system;
            if (stype != _default)
                page += "&stype=" + stype;
            if (camera != _default)
                page += "&camera=" + camera;
            if (gps == "1")
                page += "&gps=" + gps;
            if (wifi == "1")
                page += "&wifi=" + wifi;
            if (carrier != _default)
                page += "&carrier=" + carrier;
            if (markettime != _default)
                page += "&markettime=" + markettime;
            if (ram != _default)
                page += "&ram=" + ram;
            if (area != _default)
                page += "&area=" + area;
            if (cpu != _default)
                page += "&cpu=" + cpu;
            if (distingwish != _default)
                page += "&distingwish=" + distingwish;
            if (screensize != _default)
                page += "&screensize=" + screensize;
            if (color != _default)
                page += "&color=" + color;
            NavigationService.Navigate(new Uri(page, UriKind.Relative));
        }
        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            foreach (JournalEntry page in App.RootFrame.BackStack.Reverse())
            {
                if (page.Source.ToString().IndexOf("SeparatorCenter.xaml?flag=") > -1)
                    App.RootFrame.RemoveBackEntry();
            }
            string pageName = "/Page/main.xaml";
            NavigationService.Navigate(new Uri(pageName, UriKind.Relative));
        }

        private void RE(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Page/SeparatorCenter.xaml?flag=" + DateTime.Now.ToString("hhmmsss"), UriKind.Relative));
        }
    }
}